<?php
/**
 * 缓存类
 * @authors yutent (yutent@doui.cc)
 * @date    2015-07-02 10:28:16
 *
 */

class Do_Cache {

	private $dir;

    private $db;

	//缓存数据
	private static $cache = [];
    
    function __construct(){
        $this->dir = ROOT . 'data/cache/';

        if (!is_dir($this->dir)) {
            @umask(0);
            $res = @mkdir($this->dir, 0755);
            if ($res === FALSE) {
                D::log('创建cache目录[' . $this->dir . ']失败');
                return; //创建缓存目录失败
            }
        }
        $this->db = Do_Mysql::init();
    }

    /**
     * [_rw_opts 缓存设置参数]
     * @return [type] [description]
     */
    private function _rw_opts(){
    	$data = [
    		'site_name' => 'doPHP',
    		'seo_keywords' => 'doPHP',
    		'seo_description' => 'doPHP'
    	];
    	$this->set('opts', $data);
    }

    /**
     * [_rw_opts 缓存Top10新闻]
     * @return [type] [description]
     */
    private function _rw_news(){
        $file = $this->dir . 'news.dat';

        $data = [];

        $cache = serialize($data);

        file_put_contents($file, $cache);
    }





    /**
     * [get 获取cache]
     * @param  string $type [要获取的缓存类型]
     * @return [json/string/num] [返回数据]
     */
    public function get($type = ''){

    	if(isset(self::$cache[$type]) && self::$cache[$type] !== NULL){
    		return self::$cache[$type];
    	}else{
    		$file = $this->dir . $type . '.dat';
    		if(!is_file($file) || filesize($file) <= 0){
    			if(method_exists($this, '_rw_' . $type)){
		            call_user_func([$this, '_rw_' . $type]);
		        }else{
		        	return NULL;
		        }
    		}
    		if($fp = fopen($file, 'r')){
				$data = fread($fp, filesize($file));
				fclose($fp);
				self::$cache[$type] = unserialize($data);
				return self::$cache[$type];
			}
    	}

    	
    }

    function append($name, $str){
        $file = $this->dir . $name . '.dat';
        file_put_contents($file, $str . "\n", FILE_APPEND);
        return TRUE;
    }

    function get_append($name){

        if(isset(self::$cache[$name]) && self::$cache[$name] !== NULL){
            return self::$cache[$name];
        }else{
            $file = $this->dir . $name . '.dat';
            if(!is_file($file) || filesize($file) <= 0)
                return NULL;
            
            if($fp = fopen($file, 'r')){
                $str = fread($fp, filesize($file));
                fclose($fp);
                $data = explode("\n", $str);
                self::$cache[$name] = $data;
                return self::$cache[$name];
            }
        }
        

    }

    /**
     * [set 写入cache]
     * @param [string] $type [缓存类型]
     * @param [type] $data [要写入的数据]
     */
    function set($type, $data){

    	$file = $this->dir . $type . '.dat';

	    $cache = serialize($data);

        self::$cache[$type] = $data;

	    file_put_contents($file, $cache);

    }


    function update($type = ''){
        if(method_exists($this, '_rw_' . $type)){
            return call_user_func([$this, '_rw_' . $type]);
        }
        return FALSE;
    }
}